package com.ld.control.business.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.process.vo.resp.ProcessTmpRespVo;
import com.ld.control.business.task.mapper.TaskProcessTmpMapper;
import com.ld.control.business.task.mapper.TaskTmpMapper;
import com.ld.control.business.task.model.TaskProcessTmpModel;
import com.ld.control.business.task.model.TaskTmpModel;
import com.ld.control.business.task.service.TaskTmpService;
import com.ld.control.business.task.vo.req.ConfigProcessTmpReqVo;
import com.ld.control.business.task.vo.req.DragSortReqVo;
import com.ld.control.business.task.vo.req.TaskTmpReqVo;
import com.ld.control.business.task.vo.resp.TaskTmpRespVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.exception.BusinessException;
import com.ld.control.utils.CommonUtils;
import com.ld.control.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务模板表 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-05-09
 */
@Service
public class TaskTmpServiceImpl extends ServiceImpl<TaskTmpMapper, TaskTmpModel> implements TaskTmpService {

    @Autowired
    private TaskTmpMapper taskTmpMapper;

    @Autowired
    private TaskProcessTmpMapper taskProcessTmpMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Override
    public void add(TaskTmpReqVo reqVo) {
        checkAddOrUpdate(reqVo);
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        TaskTmpModel taskTmpModel = new TaskTmpModel();
        BeanUtils.copyProperties(reqVo, taskTmpModel);

        taskTmpModel.setCreateBy(currentUserAccountId);
        taskTmpModel.setCreateTime(new Date());
        taskTmpMapper.insert(taskTmpModel);
        // 更新任务模板code
        String taskTmpCode = CommonUtils.getTaskTmpCode(taskTmpModel.getId());
        taskTmpModel.setCode(taskTmpCode);
        taskTmpMapper.updateById(taskTmpModel);
    }

    @Override
    public void update(TaskTmpReqVo reqVo) {
        checkAddOrUpdate(reqVo);
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        TaskTmpModel taskTmpModel = new TaskTmpModel();
        BeanUtils.copyProperties(reqVo, taskTmpModel);
        taskTmpModel.setUpdateBy(currentUserAccountId);
        taskTmpModel.setUpdateTime(new Date());
        taskTmpMapper.updateById(taskTmpModel);
    }

    @Override
    public void deleteById(Long id) {
        // 删除任务模板
        taskTmpMapper.deleteById(id);
        // 删除任务模板喝流程模板的关联关系
        deleteByTaskTmpId(id);
    }

    @Override
    public TaskTmpRespVo getById(Long id) {
        TaskTmpRespVo taskTmpRespVo = new TaskTmpRespVo();
        TaskTmpModel taskTmpModel = taskTmpMapper.selectById(id);
        BeanUtils.copyProperties(taskTmpModel, taskTmpRespVo);
        List<ProcessTmpRespVo> processTmpRespVoList = taskProcessTmpMapper.selectListByTaskTmpId(id, null);
        taskTmpRespVo.setProcessTmpRespVoList(processTmpRespVoList);
        return taskTmpRespVo;
    }

    @Override
    public GeneratePageListVo<TaskTmpRespVo> getPageList(String name, Integer current, Integer size) {
        IPage<TaskTmpRespVo> iPage = new Page<>(current, size);
        IPage<TaskTmpRespVo> iPage1 = taskTmpMapper.selectPageList(iPage, name);
        return new GeneratePageListVo<>(iPage1);
    }

    @Override
    public void configProcessTmp(ConfigProcessTmpReqVo reqVo) {
        int serialNumber = 1;
        QueryWrapper<TaskProcessTmpModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TaskProcessTmpModel.TASK_TMP_ID, reqVo.getTaskTmpId());
        queryWrapper.orderByDesc(TaskProcessTmpModel.SERIAL_NUMBER);
        List<TaskProcessTmpModel> taskProcessTmpModelList = taskProcessTmpMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(taskProcessTmpModelList)) {
            TaskProcessTmpModel taskProcessTmpModel = taskProcessTmpModelList.get(0);
            serialNumber = taskProcessTmpModel.getSerialNumber() + 1;
        }
        TaskProcessTmpModel taskProcessTmpModel = new TaskProcessTmpModel();
        taskProcessTmpModel.setTaskTmpId(reqVo.getTaskTmpId());
        taskProcessTmpModel.setProcessTmpId(reqVo.getProcessTmpId());
        taskProcessTmpModel.setSerialNumber(serialNumber);
        taskProcessTmpMapper.insert(taskProcessTmpModel);
    }

    @Override
    public void deleteProcessTmp(Long taskProcessTmpId) {
        taskProcessTmpMapper.deleteById(taskProcessTmpId);
    }

    @Transactional
    @Override
    public void dragSort(DragSortReqVo reqVo) {
        Long taskTmpId = reqVo.getTaskTmpId();
        // 清除原始记录
        deleteByTaskTmpId(taskTmpId);

        List<Long> processTmpIdList = reqVo.getProcessTmpIdList();
        ConfigProcessTmpReqVo configProcessTmpReqVo;
        for (Long processTmpId : processTmpIdList) {
            configProcessTmpReqVo = new ConfigProcessTmpReqVo();
            configProcessTmpReqVo.setTaskTmpId(taskTmpId);
            configProcessTmpReqVo.setProcessTmpId(processTmpId);
            configProcessTmp(configProcessTmpReqVo);
        }


    }


    /**
     * 验证任务模板名称是否重复
     *
     * @param reqVo
     */
    private void checkAddOrUpdate(TaskTmpReqVo reqVo) {
        QueryWrapper<TaskTmpModel> queryWrapper = new QueryWrapper<>();
        if (reqVo.getId() != null) {
            queryWrapper.ne(TaskTmpModel.ID, reqVo.getId());
        }
        queryWrapper.eq(TaskTmpModel.NAME, reqVo.getName());
        Long count = taskTmpMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException("任务模板名称已存在！");
        }
    }

    public void deleteByTaskTmpId(Long taskTmpId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put(TaskProcessTmpModel.TASK_TMP_ID, taskTmpId);
        taskProcessTmpMapper.deleteByMap(columnMap);
    }


}
